#pragma once
#include "Singleton.h"
#include <vector>
#include <string>
#include <map>
#include "hv/HttpServer.h"
#include <functional>
#include "lib_json/json.h"
// 存储Admin网站的信息
class AdminSiteInfo{
public:
	std::string url;
	std::string token;
};

// 存储HTTP返回后解析的结果 
class HttpRet{
public:
	bool ret = false;
	Json::Value output;
};

// 存储HTTP请求的参数
class HttpParams{
public:
	std::string url;
	http_headers headers;
};
typedef std::function<HttpRet(const HttpRet&, HttpParams&)> HttpAction;
typedef std::vector<HttpAction> HttpPipeLines;

class HttpMgr : public Singleton<HttpMgr>
{
public:
	int Init(const std::string& conf_file);
	void Run();
public:
	// GET请求
	bool ReqDeviceList(Json::Value& root);
	bool ReqRealTaskList(Json::Value& root);
	bool ReqSkillList(Json::Value &root);
public:
	// POST请求
	bool ReqLogin();
	bool PostUploadTask(int iTaskType, const std::string& location, const std::string& name, const std::string& pic, int iTaskId, const std::string& taskName, const std::string& createTime);
	bool PostUploadAlarm(int iSkill, const std::string &sRule, const std::string &sSustainTime, const std::string& name, const std::string& pic, const std::string &createTime);
protected : 
	friend class Singleton<HttpMgr>;
	HttpMgr() {}
	~HttpMgr() {}
protected:
	// 封装一层
	HttpRet hvGet(const std::string &adminurl, const std::string &opt, http_headers &headers);
	HttpRet hvPost(const std::string &adminurl, const std::string &opt, const std::string &json, http_headers &headers);
	HttpRet hvUpload(const std::string &adminurl, const std::string &opt, const std::string& name, const std::string &file, http_headers &headers);
	// 管道处理系统 暂时只允许同一个管道的对象都访问 或者 只访问单个
	bool hvPipeLine(const HttpPipeLines& pipelines, std::map<std::string, HttpRet>& outputs, bool multi = false);	
private:
	std::vector<std::function<bool(HttpRet, HttpParams)>> pipeline;
private:
	hv::HttpService router;
	hv::HttpServer server;
	// 再存一个主的key
	std::string main_admin_url;
	std::map<std::string, AdminSiteInfo> admin_sites;
};

